<!-- Dom模板 -->
<template>
  <div>
    <!-- Dom内容 -->
    <div class="search animate__animated animate__slideInRight">
      <div class="top">
        <div class="top_one">
          <span
            style="fontsize: 20px"
            @click="goHOME"
            class="iconfont icon-lt"
          ></span>
        </div>
        <div class="top_left">
          <span class="iconfont icon-sousuo"></span>
          <input
            type="text"
            ref="input"
            name=""
            placeholder="2020夏令营"
            id=""
            v-model="cont"
            @input='open'
          />
        </div>
        <div class="top_right">
          <button ref="btn" @click="sousuo">搜索</button>
        </div>
      </div>
      <div class="clear">
        <p>
          <img @click="clearHistory" ref="removeBtn" src="../../../../static/img/22.png" alt="" />
        </p>
      </div>
      <!-- 搜索历史 -->
      <div class="history">
        <h3>搜索历史</h3>
        <div ref="history" class="conts">
          <span v-for="(item,index) in hisData" :key="index">
            {{item.text}}
          </span>
        </div>
      </div>
        <!-- <searchTan v-if="searchFlag"/> -->
        <searchTan :searchData="searchData" v-if="searchFlag"/>
    </div>
  </div>
</template>
<script>
// import a from './a'; // 引入组件
import searchTan from './searchTan.vue'
import * as api from '../../api/home'
import sensitiveWord from '../../js/sensitiveWord'
export default {
  name: "",
  data() {
    return {
      msg: "测试",
      arr: [],
      hisData:[],
      searchFlag:false,
      cont:'',
      searchData:[],
      searchArr:[]
    };
  },
  // 生命周期 - 创建完成（访问当前this实例）
  created() {},
  components:{
    searchTan
  },
  // 生命周期 - 挂载完成（访问DOM元素）
  mounted() {

    let input = this.$refs.input;
      let msg = input.value;
      console.log(msg);
      if (!localStorage.getItem("history")) {
        localStorage.setItem("history", JSON.stringify([]));
      }
      let arr = JSON.parse(localStorage.getItem("history"));
      localStorage.setItem("history", JSON.stringify(arr));
      this.hisData = JSON.parse(localStorage.getItem('history'))
      console.log(this.hisData);
  },
  // Vue方法定义
  methods: {
    word() {
      sensitiveWord.forEach((item, index) => {
        if (this.cont.indexOf(item) !== -1) {
            this.cont=this.cont.replace(`${item}`,"*")
        }
      });
    },
    sousuo() {
      let input = this.$refs.input;
      let msg = input.value;
      console.log(msg);
      if (!localStorage.getItem("history")) {
        localStorage.setItem("history", JSON.stringify([]));
      }
      let arr = JSON.parse(localStorage.getItem("history"));
      arr.push({
        text: msg,
      });
      localStorage.setItem("history", JSON.stringify(arr));

      this.hisData = JSON.parse(localStorage.getItem('history'))
      console.log(this.hisData);

      this.searchFlag = true

        api.searchCont({content:this.cont}).then((res)=>{
          console.log(res);
          this.searchData = res.data.code
      })
    },
    goHOME() {
      this.$router.push("/home");
    },
    clearHistory(){
       localStorage.removeItem('history');
       this.hisData = JSON.parse(localStorage.getItem('history'))
      console.log(this.hisData);
    },
    open(){
      this.searchFlag = false
      sensitiveWord.forEach((item, index) => {
        if (this.cont.indexOf(item) !== -1) {
            this.cont=this.cont.replace(`${item}`,"*")
        }
      });
    }
  },
};
</script>

<style scoped>
/* @import url(''); 引入css类 */
@import url("../../../assets/font/iconfont.css");
.search {
  width: 100vw;
  height: 100vh;
  position: absolute;
  background: rgb(255, 255, 255);
  top: 0;
  left: 0;
  z-index: 1515;
}
.animate__animated.animate__slideInRight {
  --animate-duration: 300ms;
}

:root {
  --animate-duration: 800ms;
  /* --animate-delay: 0.9s; */
}
.top {
  left: 0px;
  top: 0px;
  width: 414px;
  height: 60px;
  display: flex;
  justify-content: space-around;
  align-items: flex-end;
  /* align-items: center; */
  background-color: rgba(255, 255, 255, 100);
}
.top_one {
  height: 32px;
      margin-bottom: 7px;
    margin-left: 5px;
  line-height: 32px;
}
.top_left {
  margin-left: 19px;
  left: 19px;
  top: 50px;
  float: left;
  margin-right: 34px;
  margin-bottom: 6px;
  width: 258px;
  padding-left: 8px;
  height: 32px;
  border-radius: 100px;
  background-color: rgba(242, 242, 242, 100);
  color: rgba(191, 191, 191, 100);
  font-size: 14px;
  text-align: left;
  overflow: hidden;
  margin-bottom: 6px;
  font-family: Arial;
}
.top_left input {
  height: 32px;
  background: none;
  border: none;
  outline: none;
}
.clear {
  margin-top: 10px;
  height: 32px;
  width: 100%;
}
.clear img {
  width: 25px;
  float: right;
  margin-right: 32px;
}
.top_right {
  left: 311px;
  margin-right: 7px;
  float: left;
  top: 50px;
  width: 80px;
  height: 32px;
  line-height: 32px;
  margin-bottom: 6px;
  border-radius: 30px;
  background-color: rgba(255, 255, 255, 100);
  text-align: center;
  border: 1px solid rgba(232, 232, 232, 100);
}
.top_right button {
  height: 32px;
  text-align: center;
  line-height: 32px;
  font-size: 18px;
  border: none;
  background: none;
  color: red;
  display: inline-block;
}
.history h3 {
  margin-left: 20px;
  margin-bottom: 20px;
}
.conts {
  margin-left: 20px;
  margin-right: 20px;
  width: 374px;
}
.conts span {
  display: inline-block;
  border-radius: 30px;
  margin-right: 8px;
  padding: 5px 10px;
  background-color: rgba(255, 255, 255, 100);
  text-align: center;
  border: 1px solid rgba(232, 232, 232, 100);
}
</style>